import java.util.Stack;

//验证栈序列
//测试链接 https://leetcode.cn/problems/validate-stack-sequences/
public class ValidateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int n = pushed.length;

        int i = 0, j = 0;
        while (i < n && j < n){
            stack.push(pushed[i++]);
            while(!stack.empty() && stack.peek() == popped[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}
